//
//  RegisterController.m
//  CaiPiao
//
//  Created by l wb on 2018/5/16.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "RegisterController.h"

@interface RegisterController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *passWordTF;
    
    
    
@end

@implementation RegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"注册";
}
- (IBAction)registerBtnActionClick:(UIButton *)sender {
    if (self.userNameTF.text.length == 0) {
        [EasyTextView showInfoText:@"请输入邮箱地址！"];
        return;
    } else {
        if (![self  isValidateEmail:self.userNameTF.text]){
            [EasyTextView showInfoText:@"请输入正确的邮箱地址！"];
            return;
        }
        
    }
    if (self.passWordTF.text.length < 6||self.passWordTF.text.length >16) {
        [UIAlertController showAlertWithColorStyle:@{@"headerColor":kNavigationBarColor,@"actionTitleColor":kNavigationBarColor} Title:@"" Message:@"请输入6-16位密码" andDoneAction:nil];
        return;
    }
    
    BmobUser *bUser = [[BmobUser alloc] init];
    [bUser setUsername:self.userNameTF.text];
    [bUser setPassword:self.passWordTF.text];
    [bUser setEmail:self.userNameTF.text];
    
    
    [bUser signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            [EasyTextView showSuccessText:@"注册成功!"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        } else {
            [EasyTextView showErrorText:@"注册失败！"];
//            [EasyShowTextView showSuccessText:error.description];
        }
    }];
    
    
    
}
    
    
    
    
- (IBAction)goBackToLoginController:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (IBAction)hiddenPassWordBtn:(UIButton *)sender {
    
    self.passWordTF.secureTextEntry = sender.selected;
    sender.selected = !sender.selected;
}
- (IBAction)goTheServiceAgreementActionClick:(UIButton *)sender {
    
    
    
}
-(BOOL)isValidateEmail:(NSString *)email {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
    
    




@end
